const fs = require('fs')
const path = require('path')
const root = path.join(__dirname, '..')
const component_dir = path.join(root, 'src/components')

const lsDirFile = (cwd) => {
  const res = []
  const children = fs.readdirSync(cwd)

  children.forEach((child) => {
    const child_file = path.join(cwd, child)
    if (fs.statSync(child_file).isDirectory()) {
      const c_res = lsDirFile(path.join(cwd, child))
      res.push(...c_res)
    } else if (fs.statSync(child_file).isFile()) {
      res.push(child_file)
    }
  })

  return res
}

const lessFiles = lsDirFile(component_dir).filter((f) => path.extname(f) === '.less')

fs.writeFileSync(
  path.join(root, 'src/css/components.less'),
  lessFiles
    .map((less) => `@import '${path.relative(path.join(root, 'src/css'), less)}';`)
    .join('\n'),
  'utf-8'
)
